package com.ym.controller.soso;

import com.github.pagehelper.PageInfo;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.soso.domain.entity.AedInfoEntity;
import com.ym.soso.domain.param.AedInfoQueryParam;
import com.ym.soso.domain.param.AedInfoUpdateParam;
import com.ym.soso.domain.param.AppHealthPlatePublishParam;
import com.ym.soso.domain.vo.AedInfoListPcVO;
import com.ym.soso.service.IAedInfoSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

import static com.ym.common.constant.ValidRule.Add;
import static com.ym.common.constant.ValidRule.Update;

/**
 * AED信息Controller
 * 
 * @author ym
 * @date 2020-08-28
 */
@Api(tags = "medical-AED信息")
@RestController
@Validated
@RequestMapping("/soso/aed")
public class AedInfoController extends BaseController {

    @Autowired
    private IAedInfoSV aedInfoSV;

    @ApiOperation("查询AED信息列表")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping("/list")
    public APIResponse<PageInfo<AedInfoListPcVO>> list(@Valid AedInfoQueryParam queryParam) {
        PageDomain.startPage();
        List<AedInfoListPcVO> list = aedInfoSV.selectListPC(queryParam);
        return successPage(list);
    }

    @ApiOperation("获取AED信息详细信息")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping(value = "/info")
    public APIResponse<AedInfoEntity> info(@RequestParam Long id) {
        AedInfoEntity data = aedInfoSV.selectById(id);
        return successData(data);
    }

    @ApiOperation("新增AED信息")
    @Log(printQuery = false)
    @PostMapping("/insert")
    public APIResponse insert(@RequestBody @Validated(value = Add.class) AedInfoUpdateParam updateParam) {
        Long id = aedInfoSV.insertPC(updateParam);
        return successData(id);
    }

    @ApiOperation("修改AED信息")
    @Log(printQuery = false)
    @PostMapping("/update")
    public APIResponse update(@RequestBody @Validated(value = Update.class) AedInfoUpdateParam updateParam) {
        int result = aedInfoSV.updateByIdPC(updateParam);
        return successData(result);
    }

    @ApiOperation("发布")
    @PostMapping(value = "/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public APIResponse release(
            @RequestBody @Valid AppHealthPlatePublishParam publishParams
    ) {
        AedInfoEntity aedInfo = new AedInfoEntity();
        aedInfo.setPublishStatus(publishParams.getState());
        aedInfo.setPublishTime(new Date());
        aedInfo.setPublisher(ShiroUtil.getLoginId());
        aedInfoSV.updateByIds(aedInfo, publishParams.getIds());
        return success();
    }

    @ApiOperation("删除AED信息")
    @Log(printQuery = false)
    @PostMapping("/delete")
    public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
        int result = aedInfoSV.deleteByIds(ids);
        return successData(result);
    }
}
